package com.nexgo.payment.trans.setting;

import android.app.Dialog;
import android.os.Bundle;
import android.text.InputType;
import android.view.Gravity;
import android.widget.Toast;

import com.nexgo.payment.R;
import com.nexgo.payment.fragment.EditDialogFragment;
import com.nexgo.payment.fragment.TextDialogFragment;
import com.nexgo.payment.ui.BaseActivity;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

public abstract class SaveParameterActivity extends BaseActivity implements SaveParameterContract.View {

    protected SaveParameterContract.Presenter mPresenter;
    private EditDialogFragment mEditDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (mPresenter != null) {
            mPresenter.start();
        }
    }

    protected abstract HashMap<String, String> getDataBind();

    @Override
    public void showTip(String tip) {
        Toast.makeText(this, tip, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showInputSecurityPassword() {
        mEditDialog = new EditDialogFragment();
        mEditDialog.setDialogType(EditDialogFragment.DIALOG_TYPE_WARNING);
        mEditDialog.addTitle(getResources().getString(R.string.input_security_password));
        mEditDialog.addEditText1(InputType.TYPE_CLASS_NUMBER, 8, null, true);
        mEditDialog.setPositionText(getString(R.string.confirm));
        mEditDialog.setNegativeText(getString(R.string.cancel));
        mEditDialog.setDialogListener(new EditDialogFragment.OnDialogListener() {
            @Override
            public void onCancel() {
                mEditDialog.dismiss();
            }

            @Override
            public void onConfirm(List<String> values) {
                Iterator<String> iterator = values.iterator();
                mPresenter.judgeSecurityPassword(iterator.next());
            }
        });
        mEditDialog.show(getSupportFragmentManager(), null);
    }

    @Override
    public void setPresenter(SaveParameterContract.Presenter presenter) {
        mPresenter = presenter;
    }

    @Override
    public void showSaveParamSuccessfully() {
        if (mEditDialog != null && mEditDialog.getDialog() != null){
            Dialog dialog = mEditDialog.getDialog();
            if (dialog.isShowing()) mEditDialog.dismiss();
            mEditDialog = null;
        }
        finish();
    }

    @Override
    public void showNeedSettle() {
        TextDialogFragment dialogFragment = TextDialogFragment.newInstance();
        dialogFragment.setDialogType(TextDialogFragment.DIALOG_TYPE_WARNING);
        dialogFragment.setTitleText(getResources().getString(R.string.tip));
        dialogFragment.setContentText(getResources().getString(R.string.settle_first), Gravity.CENTER_HORIZONTAL);
        dialogFragment.setPositionText(getString(R.string.confirm));
        dialogFragment.setDialogListener(new TextDialogFragment.OnDialogListener() {
            @Override
            public void onCancel() {}

            @Override
            public void onConfirm() {
                finish();
            }
        });
        dialogFragment.show(getSupportFragmentManager(), null);
    }

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(R.anim.slide_left_in, R.anim.slide_right_out);
    }
}
